package com.cinema.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinema.bean.Cinema;
import com.cinema.service.CinemaService;
import com.cinema.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cinema")
@CrossOrigin
public class CinemaController {

    @Autowired
    private CinemaService cinemaService;

    @GetMapping("list")
    public Result getList() {
        List<Cinema> list = cinemaService.list();
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page) {
        Page result = cinemaService.page(page);
        return Result.success(result);
    }

    @PostMapping
    public Result add(@RequestBody Cinema cinema) {
        cinemaService.save(cinema);
        return Result.success("保存成功", cinema);
    }

    @DeleteMapping("/{cinema_id}")
    public Result delete(@PathVariable("cinema_id") Long cinemaId) {
        boolean b = cinemaService.removeById(cinemaId);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }
    }

    @PutMapping
    public Result edit(@RequestBody Cinema cinema) {
        boolean b = cinemaService.updateById(cinema);
        if (b) {
            return Result.success("修改成功");
        } else {
            return Result.fail(201, "修改失败");
        }
    }
}